<?php

namespace app\modules\merchant\services;

use app\common\services\BaseService;
use app\config\Constants;
use app\models\GoodsOrder;

class StatisService extends BaseService{

    public static function aggregate(){
        //昨日订单数、昨日订单金额、待调单
        return [
            'merchant_id'=>\Yii::$app->getModule("merchant")->get("user")->getId(),
            'mini_store_img'=>\Yii::$app->getModule("merchant")->get("user")->getIdentity()->mini_store_img,
            'yes_order_number'=>self::yesterdayOrderNumber(),
            'yes_order_price'=>self::yesterdayOrderPrice() ?? 0,
            'wait_adjust_count'=>self::waitAdjustOrderCount(),
        ];
    }

    public static function yesterdayOrderNumber(){
        return GoodsOrder::find()
            ->where(["date_format(add_time,'%Y-%m-%d')"=>date("Y-m-d",strtotime("-1 day"))])
            ->andWhere(['merchant_id'=>\Yii::$app->getModule("merchant")->get("user")->getId()])
            ->andWhere("status != 2")
            ->count(1);
    }

    public static function yesterdayOrderPrice(){
        return GoodsOrder::find()
            ->where(["date_format(add_time,'%Y-%m-%d')"=>date("Y-m-d",strtotime("-1 day"))])
            ->andWhere(['merchant_id'=>\Yii::$app->getModule("merchant")->get("user")->getId()])
            ->andWhere("status != 2")
            ->sum("total_price");
    }

    public static function waitAdjustOrderCount(){
        return GoodsOrder::find()
            ->andWhere(['merchant_id'=>\Yii::$app->getModule("merchant")->get("user")->getId()])
            ->andWhere(['status'=>Constants::ORDER_ADJUST])
            ->count(1);
    }

    public static function waitOrderCount(){
        return GoodsOrder::find()
            ->andWhere(['merchant_id'=>\Yii::$app->getModule("merchant")->get("user")->getId()])
            ->andWhere('status = 1')
            ->count(1);
    }
}